<template>
  <div class="detail">
    <!-- 商品分类导航 -->
    <TypeNav />

    <!-- 主要内容区域 -->
    <section class="con">
      <!-- 导航路径区域 -->
      <div class="conPoin">
        <span v-for="category in this.proThreeType" v-text="category.name"></span>
      </div>
      <!-- 主要内容区域 -->
      <div class="mainCon">
        <!-- 左侧放大镜区域 -->
        <div class="previewWrap">
          <!--放大镜效果-->
          <Zoom :skuImageList='this.skuImages'/>
          <!-- 小图列表 -->
          <ImageList :skuImageList='this.skuImages'/>

        </div>
        <!-- 右侧选择区域布局 -->
        <div class="InfoWrap">
          <div class="goodsDetail">
            <span><el-tag type="warning" class="el-icon-s-shop" @click="goChant">{{ this.proSpuInfo.merchantName }}</el-tag></span>&nbsp;
            <span><el-tag type="warning" class="el-icon-chat-dot-square" @click="goChant">联系卖家</el-tag></span>
            <h3 class="InfoName">{{this.proSkuInfo.skuTitle}}</h3>
            <p class="news">{{this.proSkuInfo.skuSubtitle}}</p>
            <div class="priceArea">
              <div class="priceArea1">
                <div class="title">价&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格</div>
                <div class="price">
                  <i>￥</i>
                  <em>{{this.proSkuInfo.price}}.00</em>
                </div>
                <div class="remark">
                  <i>累计评价 </i>
                  <em>{{commentCount}} 条</em>
                </div>
              </div>
            </div>
            <div class="support">
              <div class="supportArea">
                <div class="title">支&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;持</div>
                <div class="fixWidth">以旧换新，闲置手机回收 4G套餐超值抢 礼品购</div>
              </div>
              <div class="supportArea" v-for="address in addressList"v-if="address.defaultStatus == 1" >
                <div class="title">配 送 至</div>
                <div class="fixWidth" >{{address.province}}&nbsp;{{address.city}}&nbsp;{{address.region}}</div>
              </div>
              <div class="supportArea" v-if="addressList.length == 0">
                <div class="title">配 送 至</div>
                <div class="fixWidth" >北京 北京市 朝阳区</div>
              </div>
            </div>
          </div>

          <div class="choose">
            <div class="chooseArea">
              <div class="choosed"></div>
                <div v-for="skuInfo in this.proSkuInfos" style="float: left">
                  <el-button type="success" plain @click="reloadSkuInfo(skuInfo.skuId)">{{skuInfo.skuName}}</el-button>
                </div>
            </div>
            <br>
            <div class="cartWrap">
              <div class="controls">
                <template>
                  <el-input-number v-model="curNum" @change="handleChange" :min="1" label="描述文字"></el-input-number>
                </template>
              </div>
              <div class="add" @click="goShoppCar">
                <a>加入购物车</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 内容详情页 -->
    <section class="product-detail">
      <aside class="aside">
        <div class="tabWraped">
          <h4 class="active">相关分类</h4>
        </div>
        <div class="tabContent">
          <div class="tab-pane active">
            <ul class="partList">
              <li v-for="proSameLevel in this.proSameLevels">{{proSameLevel.name}}</li>
            </ul>
          </div>
        </div>
      </aside>
      <div class="detail">
        <div class="intro">
          <ul class="tab-wraped">
            <li :class="liActive === 0 ? 'active' : ''">
              <a href="###" @click.prevent="queryProDescImages()">
                商品介绍
              </a>
            </li>
            <li :class="liActive === 1 ? 'active' : ''">
              <a href="###" @click.prevent="queryBaseAttrs()">
                规格与包装
              </a>
            </li>
            <li :class="liActive === 2 ? 'active' : ''">
              <a href="###" @click.prevent="goComment()">
                商品评价
              </a>
            </li>
          </ul>

          <div class="tab-content">
            <div id="one" class="tab-pane active">
              <comment v-if="this.buttonStatus === 2"/>
              <ul class="goods-intro" v-if="this.buttonStatus === 1">
                <li v-for="baseAttr in this.baseAttrs">
                  {{baseAttr.attrName}}&nbsp;&nbsp;:&nbsp;&nbsp;{{parseStr(baseAttr.attrValues)}}
                </li>
              </ul>
              <div class="intro-detail" v-if="this.buttonStatus === 0">
                <img :src=proDescImage v-for="proDescImage in this.proDescImages"/>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
  import ImageList from './ImageList/ImageList'
  import Zoom from './Zoom/Zoom'
  import { mapGetters } from 'vuex'
  import axios from "axios";
  import comment from "@/pages/Detail/comment";
  import {getNickName} from "@/utils/setToken";

  export default {
    name: 'Detail',
    
    components: {
      ImageList,
      Zoom,
      comment
    },
    data(){
      return{
        curNum:1,
        skuId: ''  ,
        saleAttrs: [],  //商品销售属性
        baseAttrs: [],  //商品基本属性
        skuImages: [],  //商品大小图
        proDescImages: [],  //商品介绍图片
        proThreeType: [], //商品三级分类类别
        proSkuInfos: [], //skuInfos
        proSkuInfo: '', //skuInfo
        proSameLevels: [],  //商品同级分类
        buttonStatus:0, //下方商品详情/规格包装/商品评价按钮状态
        liActive:0,
        proSpuInfo: '', //spuInfo
        commentCount: 0,  //商品评价数量
        addressList: [],  //用户的收货地址

      }
    },
    mounted(){
      this.$store.dispatch('getDetailsList',this.$route.params.detailId)
    },
    computed:{
      ...mapGetters(['categoryView','skuInfo']),
    },
    methods:{
      // 选择数量
      handleChange(value) {
        this.curNum = value;
      },
      // 加入购物车
      goShoppCar(){
        axios({
          url: '/member/memberCart/addMyCart',
          method: 'post',
          data: {
            count: this.curNum,
            skuId: this.skuId,
          }
        }).then(response => {
          console.log(response)
          alert(response.msg)
        })
      },
      // 查询商品销售属性
      querySaleAttrs(){
        axios({
          url: '/pms/pmsAttr/queryPmsSaleAttrsBySkuId',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {
          console.log(response.data);
          this.saleAttrs = response.data;
        })
      },
      // 查询商品基本属性(规格参数)
      queryBaseAttrs(){
        this.liActive = 1;
        this.buttonStatus = 1;
        axios({
          url: '/pms/pmsAttr/queryPmsBaseAttrsBySpuId',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {
          this.baseAttrs = response.data;
        })
      },
      parseStr(attrName){
        let result = '';
        for (const attrNameElement of attrName) {
          result += attrNameElement + "  ";
        }
        return result;
      },
      //查询大小图
      querySkuImages(){
        axios({
          url: '/pms/pmsSkuImages/querySkuImagesBySkuId',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {
          console.log(response.data);
          this.skuImages = response.data;
        })
      },
      //查询商品介绍图片
      queryProDescImages(){
        this.liActive = 0;
        this.buttonStatus = 0;
        axios({
          url: '/pms/pmsSpuInfoDesc/querySpuInfoDescBySpuId',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {
          console.log(response.data);
          this.proDescImages = response.data;
        })
      },
      //查询商品三级分类
      queryThreeType(){
        axios({
          url: '/pms/pmsCategory/queryThreeType',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {
          this.proThreeType = response.data;
        })
      },
      //根据skuId查询skuInfos
      querySkuInfos(){
        axios({
          url: '/pms/pmsSkuInfo/querySkuInfosBySkuId',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {

          this.proSkuInfos = response.data;
        })
      },
      //根据skuId查询skuInfo
      querySkuInfo(){
        axios({
          url: '/pms/pmsSkuInfo/querySkuInfoBySkuId',
          method: 'get',
          params:{
            skuId: this.skuId,
            merchantName: this.proSpuInfo.merchantName,
          }
        }).then(response => {
          this.proSkuInfo = response.data;
        })
      },
      //查询商品同级分类
      querySameLevelTypes(){
        axios({
          url: '/pms/pmsCategory/querySameLevelTypes',
          method: 'get',
          params:{
            skuId: this.skuId,
          }
        }).then(response => {
          this.proSameLevels = response.data;
        })
      },
      //点击相应款式刷新页面
      reloadSkuInfo(skuId){
        this.skuId = skuId;
        this.querySkuImages();
        this.querySkuInfos();
        this.querySkuInfo();
      },
      //跳转评价页面路由
      goComment(){
        this.liActive = 2;
        this.buttonStatus = 2
        localStorage.setItem("skuId", this.skuId);
      },
      //查询店铺名称(SpuInfo)
      querySpuInfo(){
        axios({
          url: '/pms/pmsSpuInfo/querySpuInfoBySkuId',
          method: 'get',
          params: {
            skuId: this.skuId,
          }
        }).then(response => {
          this.proSpuInfo = response.data;
          this.querySkuInfo();
        })
      },
      //去往聊天室
      goChant(){
        axios({
          url:'/merchant/chat/getChatToken',
          methods: 'get'
        }).then(res=>{
          if (res.code === 200){
            sessionStorage.setItem("chatToken", this.res.data)
            sessionStorage.setItem("merchantName", this.proSpuInfo.merchantName)
            this.$router.push('/chat')
          }
        })
      },
      //查询商品评价数量
      queryProComment(){
        axios({
          url: '/pms/pmsSpuComment/queryCommentBySkuId',
          method: 'get',
          params: {
            skuId: this.skuId,
          }
        }).then(response => {
          this.commentCount = response.data.length;
        })
      },
      //查询用户收货地址
      queryAddress(){
        let username = getNickName();
        console.log(username)
        if (username === undefined || username === "" || username === null){
          return;
        }
        axios({
          url: '/member/address/list',
          method: 'get',
        }).then(response => {
          console.log(response)
          this.addressList = response.rows;
        })
      }

    },
    created() {
      // this.querySaleAttrs();
      this.skuId = localStorage.getItem("skuId")
      this.querySkuImages();
      this.querySpuInfo();
      this.queryProDescImages();
      this.queryThreeType();
      this.querySkuInfos();

      this.querySameLevelTypes();

      this.queryProComment();
      this.queryAddress();
    }
  }
</script>

<style lang="less" scoped>
  .detail {
    .con {
      width: 1200px;
      margin: 15px auto 0;

      .conPoin {
        padding: 9px 15px 9px 0;

        &>span+span:before {
          content: "/\00a0";
          padding: 0 5px;
          color: #ccc;
        }
      }

      .mainCon {
        overflow: hidden;
        margin: 5px 0 15px;

        .previewWrap {
          float: left;
          width: 400px;
          position: relative;
        }

        .InfoWrap {
          width: 700px;
          float: right;

          .InfoName {
            font-size: 14px;
            line-height: 21px;
            margin-top: 15px;
          }

          .news {
            color: #e12228;
            margin-top: 15px;
          }

          .priceArea {
            background: #fee9eb;
            padding: 7px;
            margin: 13px 0;

            .priceArea1 {
              overflow: hidden;
              line-height: 28px;
              margin-top: 10px;

              .title {
                float: left;
                margin-right: 15px;
              }

              .price {
                float: left;
                color: #c81623;

                i {
                  font-size: 16px;
                }

                em {
                  font-size: 24px;
                  font-weight: 700;
                }

                span {
                  font-size: 12px;
                }
              }

              .remark {
                float: right;
              }
            }

            .priceArea2 {
              overflow: hidden;
              line-height: 28px;
              margin-top: 10px;

              .title {
                margin-right: 15px;
                float: left;
              }

              .fixWidth {
                width: 520px;
                float: left;

                .red-bg {
                  background: #c81623;
                  color: #fff;
                  padding: 3px;
                }

                .t-gray {
                  color: #999;
                }
              }
            }


          }

          .support {
            border-bottom: 1px solid #ededed;
            padding-bottom: 5px;

            .supportArea {
              overflow: hidden;
              line-height: 28px;
              margin-top: 10px;

              .title {
                margin-right: 15px;
                float: left;
              }

              .fixWidth {
                width: 520px;
                float: left;
                color: #999;
              }
            }
          }

          .choose {
            .chooseArea {
              overflow: hidden;
              line-height: 28px;
              margin-top: 10px;

              dl {
                overflow: hidden;
                margin: 13px 0;

                dt {
                  margin-right: 15px;
                  float: left;
                }

                dd {
                  float: left;
                  margin-right: 5px;
                  color: #666;
                  line-height: 24px;
                  padding: 2px 14px;
                  border-top: 1px solid #eee;
                  border-right: 1px solid #bbb;
                  border-bottom: 1px solid #bbb;
                  border-left: 1px solid #eee;

                  &.active {
                    color: green;
                    border: 1px solid green;
                  }
                }
              }
            }

            .cartWrap {
              .controls {
                width: 48px;
                position: relative;
                float: left;
                margin-right: 15px;

                .itxt {
                  width: 38px;
                  height: 37px;
                  border: 1px solid #ddd;
                  color: #555;
                  float: left;
                  border-right: 0;
                  text-align: center;
                }

                .plus,
                .mins {
                  width: 15px;
                  text-align: center;
                  height: 17px;
                  line-height: 17px;
                  background: #f1f1f1;
                  color: #666;
                  position: absolute;
                  right: -8px;
                  border: 1px solid #ccc;
                }

                .mins {
                  right: -8px;
                  top: 19px;
                  border-top: 0;
                }

                .plus {
                  right: -8px;
                }
              }

              .add {
                float: left;
                padding-left: 140px;

                a {
                  background-color: #e1251b;
                  padding: 0 25px;
                  font-size: 16px;
                  color: #fff;
                  height: 36px;
                  line-height: 36px;
                  display: block;
                }
              }
            }
          }
        }
      }
    }

    .product-detail {
      width: 1200px;
      margin: 30px auto 0;
      overflow: hidden;

      .aside {
        width: 210px;
        float: left;
        border: 1px solid #ccc;

        .tabWraped {
          height: 40px;

          h4 {
            border-top: 3px solid #fff;
            float: left;
            line-height: 37px;
            width: 105px;
            text-align: center;
            border-bottom: 1px solid #ccc;

            &.active {
              border-top: 3px solid #e1251b;
              border-bottom: 0;
              font-weight: normal;
            }
          }
        }

        .tabContent {
          padding: 10px;

          .tab-pane {
            display: none;

            &.active {
              display: block;
            }

            &:nth-child(1) {
              .partList {
                overflow: hidden;

                li {
                  width: 50%;
                  float: left;
                  border-bottom: 1px dashed #ededed;
                  line-height: 28px;
                }
              }

              .goodsList {
                &>li {
                  margin: 5px 0 15px;
                  border-bottom: 1px solid #ededed;
                  padding-bottom: 5px;

                  .list-wrap {
                    .p-img {
                      text-align: center;

                      img {
                        width: 152px;
                      }
                    }

                    .price {
                      font-size: 16px;
                      color: #c81623;
                    }

                    .operate {
                      text-align: center;
                      margin: 5px 0;

                      a {
                        background-color: transparent;
                        border: 1px solid #8c8c8c;
                        color: #8c8c8c;
                        display: inline-block;
                        padding: 2px 14px;
                        line-height: 18px;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

      .detail {
        width: 980px;
        float: right;

        .fitting {
          border: 1px solid #ddd;
          margin-bottom: 15px;

          .kt {
            border-bottom: 1px solid #ddd;
            background: #f1f1f1;
            color: #333;
            padding: 5px 0 5px 15px;
          }

          .good-suits {
            height: 170px;
            padding-top: 10px;

            .master {
              width: 127px;
              height: 165px;
              text-align: center;
              position: relative;
              float: left;

              img {
                width: 87px;
              }

              p {
                color: #c81623;
                font-size: 16px;
                font-weight: 700;
              }

              i {
                position: absolute;
                top: 48px;
                right: -25px;
                font-size: 16px;
              }
            }

            .suits {
              width: 668px;
              height: 165px;
              float: left;

              .suitsItem {
                float: left;
                width: 127px;
                padding: 0 20px;
                text-align: center;

                img {
                  width: 120px;
                  height: 130px;
                }

                p {
                  font-size: 12px;
                }

                label {
                  display: block;
                  position: relative;

                  input {
                    vertical-align: middle;
                  }

                  span {
                    vertical-align: middle;
                  }
                }
              }
            }

            .result {
              border-left: 1px solid #ddd;
              width: 153px;
              height: 165px;
              padding-left: 20px;
              float: left;

              .num {
                font-size: 14px;
                margin-bottom: 10px;
                margin-top: 10px;
              }

              .price-tit {
                font-weight: bold;
                margin-bottom: 10px;
              }

              .price {
                color: #B1191A;
                font-size: 16px;
                margin-bottom: 10px;
              }

              .addshopcar {
                background-color: #e1251b;
                border: 1px solid #e1251b;
                padding: 10px 25px;
                font-size: 16px;
                color: #fff;
                display: inline-block;
                box-sizing: border-box;
              }
            }
          }
        }

        .intro {
          .tab-wraped {
            background: #ededed;
            // border: 1px solid #ddd;
            overflow: hidden;

            li {
              float: left;

              &+li>a {
                border-left: 1px solid #ddd;
              }

              &.active {
                a {
                  // border: 0;
                  background: #e1251b;
                  color: #fff;
                }
              }

              a {
                display: block;
                height: 40px;
                line-height: 40px;
                padding: 0 11px;
                text-align: center;
                color: #666;
                background: #fcfcfc;
                border-top: 1px solid #ddd;
                border-bottom: 1px solid #ddd;
              }
            }
          }

          .tab-content {
            .tab-pane {
              display: none;

              &.active {
                display: block;
              }

              &:nth-child(1) {
                .goods-intro {
                  padding-left: 10px;

                  li {
                    margin: 10px 0;
                  }
                }

                .intro-detail {
                  img {
                    width: 100%;
                  }
                }
              }
            }

          }
        }
      }
    }
  }
</style>